<?php
namespace App\Http\Controllers\Web;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class IndexController extends BaseController
{
    protected  $api_url = [
        'index' => ['get', 'welcome'],
        'shopindex' => ['get', 'shopindex'],
        'search'=> ['get', 'searchProduct'],
        'updateSearch'=> ['get', 'updateSearchProduct'],
    ];


    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }


    public function welcomeAction(){
//                Redis::del('data');
        if(Redis::exists('data')){
           $data= \GuzzleHttp\json_decode(Redis::get('data'),true);
        }else{
            $ret = http_web($this->api_url['index'])->send();
            Redis::set('data',json_encode($ret['data']));
            $data= \GuzzleHttp\json_decode(Redis::get('data'),true);
        }
/*
        $adv = [];

        foreach($ret['data']['adv_list']['data'] as $key => $value){
           if($value['position'] == 1) {
              $adv['ao'][$key] = $value;

           }elseif($value['position'] == 2){
               $adv['at'][$key] = $value;

           }elseif($value['position'] == 3){
               $adv['ath'][$key] = $value;

           }elseif($value['position'] == 4){
               $adv['af'][$key] = $value;

           }elseif($value['position'] == 5){
               $adv['afi'][$key] = $value;

           }elseif($value['position'] == 6){
               $adv['as'][$key] = $value;

           }

        }*/
        return view('web.welcome')->with(['info'=>$data]);
    }


    public function shopindexAction(){

//        Redis::del('shop_data');
        if(Redis::exists('shop_data')){

          $data= \GuzzleHttp\json_decode(Redis::get('shop_data'),true);
        }else{
            $ret = http_web($this->api_url['shopindex'])->send();
           Redis::set('shop_data',json_encode($ret['data']));
           $data= \GuzzleHttp\json_decode(Redis::get('shop_data'),true);
        }
        return view('web.shopindex')->with(['info'=>$data]);
    }

    //首页全文搜索
    public function searchAction(){
        $data = $this->request->all();

//        $ret = http_web($this->api_url['updateSearch'])->send($data);//导入solr
//        $ret = http_web($this->api_url['search'])->send($data);

//        return $ret;
        return $data;
         return view('web.shopindex')->with(['info'=>$ret['data']]);
     }


}